package com.cloudptt.wp.ui.message.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.cloudptt.api.product.log.Log;
import com.cloudptt.api.product.res.Resource;
import com.cloudptt.wp.R;
import com.mcptt.db.model.AddrInfo;
import com.mcptt.db.model.GroupMembInfo;
import com.mcptt.db.model.SessionInfo;
import com.mcptt.db.model.SessionReadStatus;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class GroupMemberAdapter extends BaseAdapter {

    private Context ctx = null;
    private boolean isShowStatus = false;
    private List<GroupMembInfo> membList;

    public GroupMemberAdapter(Context ctx, boolean isShowStatus,List<GroupMembInfo> membList) {
        this.ctx = ctx;
        this.isShowStatus = isShowStatus;
        this.membList = membList;
        Log.debug(this.getClass()," MessageStatusAdapter isShowStatus = " + isShowStatus + "  membList = " + membList);
    }


    @Override
    public int getCount() {
        if(membList != null){
            return membList.size();
        }else{
            return 0;
        }
    }

    public void refresh(List<GroupMembInfo> membList)
    {
        Log.debug(this.getClass(),"refresh this.conferenceInfoMap = " + this.membList);
        this.membList = membList;
    }

    @Override
    public Object getItem(int position) {
        return membList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        GroupMemberAdapter.ViewHolder viewHolder;
        GroupMembInfo info = membList.get(position);
        Log.debug(this.getClass(),"getView isShowStatus =  "+ isShowStatus + " info =  "+ info);
        String descStr = info.getDscp();
        if (convertView == null)
        {
            LayoutInflater inflater = LayoutInflater.from(ctx);
            convertView = inflater.inflate(R.layout.activity_read_status_item, null);

            viewHolder = new GroupMemberAdapter.ViewHolder();
            viewHolder.faceIv = (ImageView) convertView.findViewById(R.id.faceIv);
            viewHolder.nameTv = (TextView) convertView.findViewById(R.id.nameTv);
            viewHolder.statusTv = (TextView) convertView.findViewById(R.id.statusTv);

            convertView.setTag(viewHolder);
        }
        else
        {
            viewHolder = (GroupMemberAdapter.ViewHolder) convertView.getTag();
        }
        String addrId = info.getAddrId();
        boolean isConnected = info.isConnected();
        Log.debug(this.getClass(),"getView addrId =  "+ addrId + " isConnected = " + isConnected);
        if(isConnected)
        {
            if(info.isSys()
                    || info.isDac())
            {
                viewHolder.faceIv.setImageResource(R.drawable.icon_main_user_dac_face);

            }else if(info.isFuncType())
            {
                viewHolder.faceIv.setImageResource(R.drawable.icon_main_user_func_face);

            }
            else{
                viewHolder.faceIv.setImageResource(R.drawable.icon_main_user_face);
            }
        }
        else
        {
            if(info.isSys()
                    || info.isDac())
            {
                viewHolder.faceIv.setImageResource(R.drawable.icon_main_user_dac_offline_face);

            }else if(info.isFuncType())
            {
                viewHolder.faceIv.setImageResource(R.drawable.icon_main_user_func_offline_face);

            }
            else{
                viewHolder.faceIv.setImageResource(R.drawable.icon_main_user_offline_face);
            }
        }

        if(isConnected)
        {
            String str = this.isConnectStr(isConnected);
            viewHolder.statusTv.setText(str);
            viewHolder.statusTv.setVisibility(View.VISIBLE);
        }
        else
        {
            viewHolder.statusTv.setVisibility(View.GONE);
        }

        viewHolder.nameTv.setText(descStr);
        return convertView;
    }

//    private boolean isConnected(String addrId)
//    {
//        Log.debug(this.getClass(),"isConnectStr addrId =  "+ addrId + " conferenceInfoMap =  "+ conferenceInfoMap);
//        if(conferenceInfoMap != null
//                && conferenceInfoMap.get(addrId) != null)
//        {
//            boolean isConnect = conferenceInfoMap.get(addrId);
//            return isConnect;
//        }
//        return false;
//    }

    private String isConnectStr(boolean isConnect)
    {
        Log.debug(this.getClass(),"isConnectStr isConnect =  "+ isConnect);
        if (isConnect) {
            return ctx.getString(R.string.group_call_connected);
        }
        return ctx.getString(R.string.group_call_unconnected);
    }

    private class ViewHolder {
        public ImageView faceIv;
        public TextView nameTv;
        public TextView statusTv;
    }
}
